package com.sky.d_design_pattern;

public class FutureData implements Data {
	
	protected RealData realData = null;		// FutureData是RealData的包装
	protected boolean isReady = false;		
	
	public synchronized void setRealData(RealData realData) {
		if(isReady){
			return ;
		}
		this.realData = realData;
		isReady = true;
		notifyAll(); 	//RealData 已经被注入，通知getResult
	}

	@Override
	public synchronized String getResult() {		//会等待RealData构造完成
		while(!isReady){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return realData.result;		
	}

}
